package com.numbfish.ch01_ex01_bean_resister.config;


import com.numbfish.ch01_ex01_bean_resister.pojo.Person;
import org.springframework.context.annotation.*;
import org.springframework.core.type.AnnotatedTypeMetadata;

@Configuration
public class CustomConfig03Conditional {
    // 创建两个bean 曹操和刘备
    // 以以上两个bean为条件进行bean注册孙权
    @Bean
    public Person caocao() {
        return new Person("曹操", 54);
    }

    @Bean
    public Person liubei() {
        return new Person("刘备", 40);
    }

    @Bean
    // Conditional传入的是一个接口实现类数组 Class<? extends Condition>[]
    @Conditional(CustomCondition.class)
    public Person sunquan() {
        return new Person("孙权", 30);
    }


}

class CustomCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Object caocao = context.getBeanFactory().getBean("caocao");
        Object liubei = context.getBeanFactory().getBean("liubei");
        if (caocao != null && liubei != null) return true;
        return false;
    }
}
